from tkinter.messagebox import showerror, showwarning, showinfo, Message
import pystray
import sys
from PIL import Image
from pyexpat.errors import messages
from pystray import Icon
from pystray import Menu, MenuItem
from script import run

import threading
from tkinter import Tk, Entry, Label, Button, ttk
import script

components = {
    'gapLabel' : None,
    'win' : None,
    'icon' : None
}

started = False

class Win:
    def __init__(self):
        self.icon_path = 'icon.ico'
        self.root = Tk()
        self.root.iconbitmap(self.icon_path)
        self.root.title('新财校园网自动登录')
        self.root.geometry('300x100')
        self.root.minsize(300, 200)
        self.root.maxsize(400, 200)
        self.root.wm_iconbitmap(self.icon_path)

    def on_exit(self):
        self.root.withdraw()

    def startLoad(self, gap):
        gap = gap
        if (not gap.isnumeric()):
            showerror('输入错误!', '请输入数字~')
            return
        elif (float(gap) < 2):
            showwarning('太快辣！', '至少>=1s好吗!')
            return
        else:
            global started
            if (started):   # 登录成功才会显示信息
                components['icon'].notify(script.stateMessage, '登录消息')
            else:
                started = True
            threading.Thread(target=run, args=(gap),daemon=True).start()
            components['gapLabel'].config(text=f'当前检测间隔(s): {gap}')
            components['gapLabel'].config(foreground='green')




class AppIcon:
    def __init__(self):
        self.menu = (
            MenuItem('显示状态信息', action=lambda :self.showState()),
            MenuItem('登录账号信息', action=lambda :self.showAccount()),
            MenuItem('最小化托盘', components['win'].on_exit),
            MenuItem('最大化窗口', self.show_win),
            MenuItem('退出程序', self.quit_win),
        )
        self.image = Image.open('icon.ico')

        self.root = Icon('HollowKnight', self.image, '丝之歌到底要什么时候出啊!', self.menu)

    def notify(self, st1, st2):
        self.root.notify(st1, st2)

    def showState(self):
        if (not started):
            stateTitle = '未开启'
            stateDetail = '开发者: Sy_'
        else:
            stateTitle = '运行中'
            stateDetail = '开发者: Sy_'
        self.root.notify(stateDetail, stateTitle)

    def showAccount(self):
        stateTitle = '账号:' + script.username
        stateDetail = '开发者: Sy_'
        self.notify(stateDetail, stateTitle)

    # 退出程序
    def quit_win(self):
        win.root.destroy()
        run.stop()

    def show_win(self):
        win.root.deiconify()

win = Win()
components['win'] = win
icon = AppIcon()
components['icon'] = icon





# ------------------------------------- 控件 -------------------------------------------------------
label = ttk.Label(win.root, text='输入检测间隔(s):')
label.pack(pady=(20, 10))

entry = ttk.Entry(win.root, width=10)
entry.pack(pady=10)

button = ttk.Button(win.root, text='开启自动登录', command=lambda : win.startLoad(entry.get()))
button.pack(pady=(5, 10))

gapLabel = ttk.Label(win.root, text='当前检测间隔(s): ', foreground='red')
gapLabel.pack(pady=10)
# ------------------------------------- 控件 -------------------------------------------------------


# 注册组件
components['gapLabel'] = gapLabel


# 重新定义窗口关闭事件
win.root.protocol('WM_DELETE_WINDOW', win.on_exit)

# 为最小化窗口开一个线程
threading.Thread(target=icon.root.run, daemon=True).start()

# tk主循环
win.root.mainloop()



